package com.ttbj.product.bean.dto;

import java.io.IOException;
import java.io.Serializable;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.ttbj.exception.WebException;
import com.ttbj.util.ResultUtil;

import io.netty.util.internal.StringUtil;
import net.sf.json.JSONObject;

@JsonInclude(Include.NON_NULL)
public class UserDto implements Serializable {
	private static final Logger logger = LoggerFactory.getLogger(UserDto.class);
	private static final long serialVersionUID = 6972626064095880945L;
	private Long id;

	private String city;

	private String country;

	private String headImgUrl;

	private String name;

	private String nickname;

	private String phone;

	private String province;

	private String remark;

	private Integer sexId;

	private String openId;

	private String sessionKey;

	public UserDto(String openId) {
		this.openId = openId;
	}
	public UserDto(Long userId) {
		this.id = userId;
	}
	
	public UserDto distrSex (String gender){
		if (gender != null) {
			if (gender.equals("1")) {
				setSexId(1);
			} else if (gender.equals("2")) {
				setSexId(2);
			} else {
				setSexId(0);
			}
		} else {
			setSexId(0);
		}
		return this;
	}
	
	public UserDto netOpenId(String url) throws Exception{
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse httpResponse = null;
		try {
			httpClient = getHttpClient();// 创建默认的httpClient实例
			HttpGet get = new HttpGet(url);// 用get方法发送http请求
			httpResponse = httpClient.execute(get);
			HttpEntity entity = httpResponse.getEntity();
			if (null == entity) {
				return this;
			}
			String result = EntityUtils.toString(entity);
			logger.info("获取openid请求获取的字符串：{}", result);
			JSONObject resultJson = JSONObject.fromObject(result);
			if(resultJson.isNullObject()){
				ResultUtil.throwExcepion(ResultUtil.createFail("messages",108), "错误：未获取到微信用户信息(resultJson)");
			}
			if(!resultJson.containsKey("session_key") ||!resultJson.containsKey("openid")){
				ResultUtil.throwExcepion(ResultUtil.createFail("messages",108), "错误：未获取到微信用户信息(openId或是session_key)");
			}
			String session_key = resultJson.getString("session_key");
			String openid = resultJson.getString("openid");
			logger.info("session_key :" + session_key + "openid" + openid);
			this.setOpenId(openid);
			this.setSessionKey(session_key);
		} finally {
			if (httpResponse != null) {
				httpResponse.close();
			}
			if (httpClient != null) {
				closeHttpClient(httpClient);
			}
		}
		return this;
	}
	
	public UserDto() {
	}
	
	public UserDto(String openId, String phone) {
	 this.openId = openId;
	 this.phone = phone;
	}
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getHeadImgUrl() {
		return headImgUrl;
	}

	public void setHeadImgUrl(String headImgUrl) {
		this.headImgUrl = headImgUrl;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Integer getSexId() {
		return sexId;
	}

	public void setSexId(Integer sexId) {
		this.sexId = sexId;
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public String getSessionKey() {
		return sessionKey;
	}

	public void setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	private CloseableHttpClient getHttpClient() {
		return HttpClients.createDefault();
	}

	private void closeHttpClient(CloseableHttpClient client) throws IOException {
		if (client != null) {
			client.close();
		}
	}
	

}
